package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.laws.LindiLawsQueryVO;
import com.minimalist.common.entity.lindi.vo.laws.LindiLawsVO;
import com.minimalist.common.feign.client.lindi.LindiLawsFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_法规管理")
@RequestMapping("/basic/lindi/laws")
public class LindiLawsController{

    @Autowired
    private LindiLawsFeignClient feignClient;

    @PostMapping("/addLaws")
    @SaCheckPermission("lindi:laws:add")
    @Operation(summary = "添加法规")
    public R<?> addLaws(
            @RequestBody @Validated(Add.class) LindiLawsVO lawsVO){
        lawsVO.setCreateId(StpUtil.getLoginIdAsLong());
        lawsVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        lawsVO.setCreateTime(now);
        lawsVO.setUpdateTime(now);
        return feignClient.addLaws(lawsVO);
    }

    @DeleteMapping("/deleteLawsByLawsId")
    @SaCheckPermission("lindi:laws:delete")
    @Operation(summary = "删除法规 -> 根据法规ID删除")
    public R<?> deleteLawsByLawsId(
            @RequestParam("LawsId")
            @NotNull(message = "法规ID不能为空")
            @Parameter(name = "LawsId", required = true
                    , description = "法规ID") Long lawsId){
        return feignClient.deleteLawsByLawsId(lawsId);
    }

    @PutMapping("/updateLawsByLawsId")
    @SaCheckPermission("lindi:laws:update")
    @Operation(summary = "修改法规")
    public R<?> updateLawsByLawsId(
            @RequestBody @Validated(Update.class) LindiLawsVO lawsVO){
        lawsVO.setCreateId(StpUtil.getLoginIdAsLong());
        lawsVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        lawsVO.setCreateTime(now);
        lawsVO.setUpdateTime(now);
        return feignClient.updateLawsByLawsId(lawsVO);
    }

    @GetMapping("/getPageLawsList")
    @SaCheckPermission("lindi:laws:get")
    @Operation(summary = "查询法规列表(分页)")
    public R<PageResp<LindiLawsVO>> getPageLawsList(LindiLawsQueryVO queryVO){
        System.out.println("queryVO = " + queryVO);
        System.out.println("feignClient.getPageLawsList(queryVO) = " + feignClient.getPageLawsList(queryVO));
        return feignClient.getPageLawsList(queryVO);
    }

    @GetMapping("/getLawsByLawsId/{LawsId}")
    @SaCheckPermission("lindi:laws:get")
    @Operation(summary = "根据法规ID查询法规")
    public R<LindiLawsVO> getLawsByLawsId(
            @PathVariable(value = "LawsId")
            @NotNull(message = "法规ID不能为空")
            @Parameter(name = "LawsId", description = "参数ID"
                    , required = true) Long lawsId){
        return feignClient.getLawsByLawsId(lawsId);
    }
}